<?php

namespace App\Admin\Controllers;

use App\Models\Visitor;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class VisitorController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '访客信息';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Visitor());
        $grid->model()->orderBy('id', 'desc');
        $grid->filter(function ($filter) {
            $filter->disableIdFilter();
            $filter->column(1/2, function ($filter) {
                $filter->like('name', '姓名');
            });
            $filter->column(1/2, function ($filter) {
                $filter->like('mobile', '手机号码');
            });
        });
        $grid->column('id', __('编号'));
        $grid->column('name', __('访客姓名'));
        $grid->column('mobile', __('手机号码'));
        $grid->column('openid', __('OPENID'));
        $grid->column('created_at', __('创建时间'))->display(function($time) {
            return date('Y-m-d H:i:s', strtotime($time));
        });
        $grid->column('updated_at', __('修改时间'))->display(function($time) {
            return date('Y-m-d H:i:s', strtotime($time));
        });
        $grid->disableCreateButton();
        $grid->actions(function (Grid\Displayers\Actions $actions) {
            $actions->disableView();
            $actions->disableEdit();
        });
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Visitor::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('name', __('Name'));
        $show->field('mobile', __('Mobile'));
        $show->field('openid', __('Openid'));
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Visitor());

        $form->text('name', __('Name'));
        $form->mobile('mobile', __('Mobile'));
        $form->text('openid', __('Openid'));

        return $form;
    }
}
